﻿
#region Using Directives.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Windows.Forms;
using GDFlacTool.Common;
using GDFlacTool.Properties;

#endregion

namespace GDFlacTool.Forms
{
    public partial class FormPreviewFileName : Form
    {
        private readonly IList<Track> _tracks;
        private readonly MaskHelperCollection _masks;
        private readonly bool _replaceUnderscores;
        private readonly CaseType _caseType;
        private readonly string _currentPath;
        private readonly string _currentExtension;

        public FormPreviewFileName(IList<Track> tracks, MaskHelperCollection masks, bool replaceUnderscores, CaseType caseType, string currentPath, string extension)
        {
            InitializeComponent();

            Width = PreviewFileNameSettings.Default.FormWidth;
            Height = PreviewFileNameSettings.Default.FormHeight;

            _tracks = tracks;
            _masks = masks;
            _replaceUnderscores = replaceUnderscores;
            _caseType = caseType;
            _currentPath = currentPath;
            _currentExtension = extension;

            LoadMaskCombo();

            int index = cboFileNameMask.Items.IndexOf(_masks.Mask);

            if (index >= 0)
            {
                cboFileNameMask.SelectedIndex = index;
            }
            else
            {
                cboFileNameMask.Text = _masks.Mask;
            }

            objectListView.Columns[1].Width = objectListView.Width / 2;
        }

        public string GetCurrentMask
        {
            get { return cboFileNameMask.Text; }
        }

        #region Form Events.

        private void FormPreviewFileName_Load(object sender, EventArgs e)
        {
            int index = cboFileNameMask.Items.IndexOf(_masks.Mask);

            if(index >= 0)
            {
                cboFileNameMask.SelectedIndex = index;
            }
            else
            {
                cboFileNameMask.Text = _masks.Mask;
            }
        }

        private void FormPreviewFileName_FormClosing(object sender, FormClosingEventArgs e)
        {
            PreviewFileNameSettings.Default.FormHeight = Height;
            PreviewFileNameSettings.Default.FormWidth = Width;

            PreviewFileNameSettings.Default.Save();
        }

        #endregion

        private void btnMaskEditor_Click(object sender, EventArgs e)
        {
            var frm = new FormMaskEditor(cboFileNameMask.Text, _currentPath, RenameType.FILENAME, _currentExtension);

            DialogResult result = frm.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                cboFileNameMask.Items.Clear();

                LoadMaskCombo();

                int index = cboFileNameMask.Items.IndexOf(frm.Mask);

                if (index >= 0)
                {
                    cboFileNameMask.SelectedIndex = index;
                }
                else
                {
                    cboFileNameMask.Text = frm.Mask;
                }

                _masks.Mask = frm.Mask;

                RefreshMask();
            }
        }

        private void RefreshMask()
        {
            CreateNewFileNames();
            objectListView.SetObjects(_tracks);
            objectListView.Columns[0].Text = string.Format("Before Rename - {0}", _tracks[0].OldPath);
            objectListView.Columns[1].Text = string.Format("After Rename - {0}", _tracks[0].NewPath);

            txtResultPreview.Text = string.Format("{0}{1}", ShowPreView(_masks.Mask), _currentExtension);
        }

        private void CreateNewFileNames()
        {
            foreach (Track track in _tracks)
            {
                TrackRename trackRename = new TrackRename(track.FileName, track.FilePath, track.FullPath, _replaceUnderscores, _caseType);

                track.RenameTrack(_masks, trackRename);
            }
        }

        private void LoadMaskCombo()
        {
            StringCollection masks = MaskEditorSettings.Default.SavedRenameMasks;

            if (masks != null)
            {
                foreach (var mask in masks)
                {
                    cboFileNameMask.Items.Add(mask);
                }
            }
        }

        private string ShowPreView(string mask)
        {
            if (string.IsNullOrEmpty(mask))
                return string.Empty;

            string result = mask;

            MaskHelperCollection helper = new MaskHelperCollection();

            foreach (MaskHelper maskHelper in helper)
            {
                result = result.Replace(maskHelper.Mask, maskHelper.Example);
            }

            return result;
        }

        private void FormPreviewFileName_Resize(object sender, EventArgs e)
        {
            objectListView.BeginUpdate();

            objectListView.Columns[1].Width = objectListView.Width / 2;

            panButtons.Left = Width - (panButtons.Width + 15);
            
            lineBottom.Width = Width - 18;
            
            objectListView.EndUpdate();
        }

        private void objectListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            objectListView.SelectedIndex = -1;
            
        }

        private void cboFileNameMask_TextChanged(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(cboFileNameMask.Text))
            {
                _masks.Mask = cboFileNameMask.Text;

                RefreshMask();
            }
        }
    }
}
